<?php
class ApiCenter_ProductApi_Model_Api2_Product_Approval_Rest_Admin_V1 extends  ApiCenter_ProductApi_Model_Api2_Product_Approval_Rest{
    
    
    protected function _update($filteredData){
        $approval = (int)$filteredData['approval'];
        $id = $this->getRequest()->getParam("id");
        $product = Mage::getModel("catalog/product")->load($id);
        $type = $product->getTypeId();
        try {
            
            if($type == "simple"){
                if(isset($approval)&&$approval<2){
                    $product->setApproval($approval);
                    $product->setUpdatedAt(now());
                    $product->save();
                }
            }elseif($type == "configurable"){
                if(isset($approval)&&$approval<2){
                    $product->setApproval($approval);
                    $product->setUpdatedAt(now());
                    $product->save();
                    if($childIds=$this->_loadchildproduct($product)){
                        foreach ($childIds as $cid){
                            $cproduct = Mage::getModel("catalog/product")->load($cid);
                            $cproduct->setApproval($approval);
                            $cproduct->setUpdatedAt(now());
                            $cproduct->save();   
                        }
                    }
                }
             
            }
  
            $res['status'] = 1;
            $res['info'] = "提交成功";
        } catch (Exception $e) {
            $res['status'] = 0;
            $res['info'] = "提交失败，请重试!";
        }
        $this->_render($res);
    }
      /**
     * 加载子产品
     * */
    protected function _loadchildproduct($product){
        $childIds =array();
        $products = $product->getTypeInstance(true)
        ->getUsedProducts(null, $product);
        foreach($products as $_product){
            $childIds[] = $_product->getId();
        }
        return !empty($childIds)?$childIds:false;
    }
}